<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use App\Models\MainDB\Badge;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class BadgeRepository {


    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addBadgeType($param) {
        $addConfig = app('model_badge_type')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delBadgeType($idArr = []) {
        $res = app('model_badge_type')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveBadgeType($where,$param) {
        $saveConfig = app('model_badge_type')->where($where)->update($param);

        if ($saveConfig!==false) return true;
        throw new BasicException(93047);
    }

    /**
     * 单条
     */
    public function getBadgeType($where) {
        $result = app('model_badge_type')->where($where)->first();

        return $result;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getBadgeTypeQueryList(Array $params) {
        $whereArr = [];
        if (!empty($params["status"])) {
            $whereArr["status"] = $params["status"];
        }

        $whereFunc = function ($query) use ($params) {
            if (!empty($params["keyword"])) {
                $query->where('name', 'like', "%" . $params['keyword'] . "%");
            }
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_badge_type')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addBadge($param) {
        $addConfig = app('model_badge')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delBadge($idArr = []) {
        $res = app('model_badge')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveBadge($where,$param) {
        $saveConfig = app('model_badge')->where($where)->update($param);

//        if ($saveConfig) return $saveConfig;
//        throw new BasicException(93047);
    }

    /**
     * 单条
     */
    public function getBadge($where) {
        $result = app('model_badge')->where($where)->first();

        return $result;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getBadgeQueryList(Array $params) {
        $whereArr = [];
        if (!empty($params["status"])) {
            $whereArr["status"] = $params["status"];
        }
        if (!empty($params["type_id"])) {
            $whereArr["type_id"] = $params["type_id"];
        }

        $whereFunc = function ($query) use ($params) {
            if (!empty($params["keyword"])) {
                $query->where('name', 'like', "%" . $params['keyword'] . "%");
            }
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_badge')->with("badge_type:id,name")->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addBadgeUser($param) {
        $addConfig = app('model_badge_user')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }


    /**
     * 单条
     */
    public function getBadgeUser($where) {
        $result = app('model_badge_user')->where($where)->first();

        return $result;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getBadgeUserQueryList(Array $params) {
        $whereArr = [];
        if (!empty($params["user_code"])) {
            $whereArr[] = ["t2.user_code", "=", $params["user_code"]];
        }
        if (!empty($params["nickname"])) {
            $whereArr[] = ["t2.nickname", "like", "%". $params["nickname"] . "%"];
        }

        if (!empty($params["type_id"])) {
            $whereArr[] = ["t3.type_id", "=", $params["type_id"]];
        }

        if (!empty($params["badge_name"])) {
            $whereArr[] = ["t3.name", "like", "%". $params["badge_name"] . "%"];
        }


        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $badgeUserModel = app('model_badge_user');
        $userDataModel = app('model_user_data');
        $badgeModel = app('model_badge');
        $badgeTypeModel = app('model_badge_type');

        $result = app('model_badge_user')
            ->from($badgeUserModel->getTable() . " as t1")
            ->select([
                "t1.id",
                "t2.nickname",
                "t2.user_code",
                DB::raw("t3.name as badge_name"),
                "t3.picture",
                DB::raw("t4.name as badge_type_name"),
                "t1.add_time"
            ])
            ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->leftJoin(DB::raw($badgeModel->getTable() . " as t3"), "t1.badge_id", "=", "t3.id")
            ->leftJoin(DB::raw($badgeTypeModel->getTable() . " as t4"), "t3.type_id", "=", "t4.id")
            ->where($whereArr)->where($whereFunc)->orderBy('t1.id','desc')->paginate($perPage, ['*'], '', $page);

        foreach ($result as $item) {
            $item->add_time = date("Y-m-d H:i:s", $item->add_time);
        }

        return $result;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getBadgeExchangeQueryList(Array $params) {
        $whereArr = [];
        if (!empty($params["user_code"])) {
            $whereArr[] = ["t2.user_code", "=", $params["user_code"]];
        }


        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $badgeExchangeModel = app('model_badge_exchange');
        $userDataModel = app('model_user_data');

        $result = app('model_badge_user')
            ->from($badgeExchangeModel->getTable() . " as t1")
            ->select([
                "t1.id",
                "t1.goods_name",
                "t1.number",
                "t1.goods_id",
                "t2.nickname",
                "t2.user_code",
                "t1.add_time"
            ])
            ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
            ->where($whereArr)->where($whereFunc)->orderBy('t1.id','desc')->paginate($perPage, ['*'], '', $page);

        foreach ($result as $item) {
            $item->add_time = date("Y-m-d H:i:s", $item->add_time);
        }

        return $result;
    }

}